const webpack = require('webpack');
const path = require('path');

function resolve(dir) {
    return path.join(__dirname, dir)
}
const isProd = process.env.NODE_ENV === 'production'
const NODE_ENV = isProd ? "pro" : "dev"
const httpUrl = isProd ? require('./app.json')['http-pro'] : require('./app.json')['http-dev']
const UNI_PLATFORM = process.env. UNI_PLATFORM
const assetsCDN = {
    externals: {
        vue: 'Vue',
        'vue-router': 'VueRouter',
        'ant-design-vue': "'antDesignVue'",
        vuex: 'Vuex',
        axios: 'axios'
    },
    css: [
        "//cdn.jsdelivr.net/npm/ant-design-vue@1.7.6/dist/antd.min.css"
    ],
    // https://unpkg.com/browse/vue@2.6.10/2.6.11
    js: [
        '//cdn.jsdelivr.net/npm/vue@2.6.11/dist/vue.min.js',
        '//cdn.jsdelivr.net/npm/vue-router@3.2.0/dist/vue-router.min.js',
        '//cdn.jsdelivr.net/npm/vuex/dist/vuex.min.js',
        '//cdn.jsdelivr.net/npm/axios@0.21.1/dist/axios.min.js',
        "//cdn.jsdelivr.net/npm/ant-design-vue@1.7.6/dist/antd.min.js"
    ]
}
const vueConfig = {
    productionSourceMap: false,
    lintOnSave: false,
    devServer: {
        // 开发模式下的 端口号
        port: 8000,
        // 本地 跨域设置
        proxy: {
            '/api': {
                target: 'http://test.yilian.miaoshou.com:7001',
                changeOrigin: true,
                pathRewrite: {
                    "^/api": ""
                }
            }
        }
    },
    configureWebpack: {
        // webpack plugins
        plugins: [
            // Ignore all locale files of moment.js
            // new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),
            new webpack.DefinePlugin({
                APP_VERSION: `"${require('./package.json').version}"`,
                BUILD_DATE: JSON.stringify(new Date().toLocaleString()),
                APP_ENV: `"${NODE_ENV}"`,
                APP_BASE: `"${httpUrl}"`,
                UNI_PLATFORM: `"${UNI_PLATFORM}"`
            })
        ],
        // if prod, add externals
        externals: isProd ? assetsCDN.externals : {}
    },
    chainWebpack(config) {
        config.resolve.alias.set('@', resolve('src'));
        config.resolve.alias.set('_c', resolve('src/components'));
        if (isProd) {
            config.plugin('html').tap(args => {
                args[0].cdn = assetsCDN
                return args
            })
        }
    },

}


module.exports = vueConfig